﻿#include "opedb.h"
#include <iostream>

OpeDB::OpeDB(QObject* parent) : QObject(parent)
{
	std::cout << "数据库已装载" << std::endl;
	m_db = QSqlDatabase::addDatabase("QSQLITE");
}

OpeDB::~OpeDB() {
	std::cout << "数据库已卸载" << std::endl;
	m_db.close();
}

bool OpeDB::handleRegist(const char* name, const char* pwd)
{
	if (name == NULL || pwd == NULL) {
		qDebug() << "用户名或密码为空";
		return false;
	}
	//拼接插入语句
	QString data = QString("insert into usrInfo(name, pwd) values(\'%1\', \'%2\')").arg(name).arg(pwd);
	qDebug() << "DB查询语句为：" << data;
	QSqlQuery query;
	// query whether there are dupilicate items
	return query.exec(data);
}

bool OpeDB::handleLogin(const char* name, const char* pwd)
{
	//对形参有效性判断
	if (*name == 0 || *pwd == 0) {
		qDebug() << "用户名或密码为空";
		return false;
	}
	//拼接查询语句
	QString data = QString("select * from usrInfo where name=\'%1\' and pwd=\'%2\' and online=0").arg(name).arg(pwd);
	qDebug() << "登录时DB查询语句为：" << data;
	QSqlQuery query;
	query.exec(data);
	//获得该记录返回true，否则返回false
	if (query.next()) {
		data = QString("update usrInfo set online=1 where name=\'%1\' and pwd=\'%2\'").arg(name).arg(pwd);
		query.exec(data);
		return true;
	}
	else return false;
}

void OpeDB::handleOffline(const char* name, const char* pwd)
{
	//对形参有效性判断 QString 空串即为'\0'
	if (*name == 0 || *pwd == 0) {
		qDebug() << "登出时提示：未登入有效用户";
		return;
	}
	//拼接查询语句
	QString data = QString("update usrInfo set online=0 where name=\'%1\' and pwd=\'%2\'").arg(name).arg(pwd);
	qDebug() << "登出时DB执行语句为：" << data;
	QSqlQuery query;
	query.exec(data);
}

int OpeDB::handleSearchUsr(const char* name) {
	QString data = QString("select online from usrInfo where name = \'%1\'").arg(name);
	QSqlQuery query;
	query.exec(data);
	if (!query.next()) {
		return -1;
	}
	else {
		return query.value(0).toInt();
	}
}

void OpeDB::handleRecover() {
	QSqlQuery query;
	query.exec("update usrInfo set online = 0 where online = 1");
	qDebug() << "已完成数据库恢复";
}

QStringList OpeDB::handleAllOnline() {
	QString data = QString("select name from usrInfo where online = 1");
	QSqlQuery query;
	query.exec(data);
	QStringList result;
	result.clear();
	while (query.next()) {
		result.append(query.value(0).toString());
	}
	return result;
}

int OpeDB::handleAddFriend(const char* perName, const char* name)
{
	if (perName == NULL || name == NULL) {
		return -2;
	}
	QString data = QString("select * from friend where"
		"(id = (select id from usrInfo where name = \'%1\') and friendId = (select id from usrInfo where name = \'%2\'))"
		"or (id = (select id from usrInfo where name = \'%3\') and friendId = (select id from usrInfo where name = \'%4\'))")
		.arg(perName).arg(name).arg(name).arg(perName);
	QSqlQuery query;
	query.exec(data);
	if (query.next()) {	//已经是好友关系
		return 2;
	}
	else {	//非好友关系，先判断对方是否在线
		return handleSearchUsr(perName);
	}
}

int OpeDB::addFriend(const char* perName, const char* name)
{
	if (perName == NULL || name == NULL) {
		return -1;
	}
	QString data = QString("insert into friend(id, friendId) values "
		"((select id from usrInfo where name = \'%1\'),"
		"(select id from usrInfo where name = \'%2\'))").arg(name).arg(perName);
	QSqlQuery query;
	return query.exec(data);
}

//单例模式，只能静态调用获得一个实例
OpeDB& OpeDB::getInstance()
{
	static OpeDB instance;
	return instance;
}

void OpeDB::init()
{
	std::cout << "数据库初始化" << std::endl;
	m_db.setHostName("localhost");
	m_db.setDatabaseName("C:\\Users\\catmas\\source\\repos\\TcpServer\\TcpServer\\cloud.db");
	if (m_db.open()) {
		QSqlQuery query;
		query.exec("select* from usrInfo");
		while (query.next()) {
			QString data = QString("%1,%2,%3").arg(query.value(0).toString())
				.arg(query.value(1).toString())
				.arg(query.value(2).toString());
			qDebug() << data;
		}
	}
	else {
		QMessageBox::critical(NULL, "open database", "open database failed");
	}
}